package com.bw.shiro.authentication;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

public class Authentication {
    @Test
    public void textAuthentivation(){
        SimpleAccountRealm realm = new SimpleAccountRealm();//创建一个域
        realm.addAccount("张三","123");//赋值

        DefaultSecurityManager manager = new DefaultSecurityManager();//创建 核心  shiro核心
        manager.setRealm(realm);//设置使用的realm //交给核心管理
        SecurityUtils.setSecurityManager(manager);//安全管理器   把核心放到shiro的环境里
        Subject subject = SecurityUtils.getSubject();//主体 //获取主体   创建主体
        //用户登录时候的账号密码
        UsernamePasswordToken token = new UsernamePasswordToken("张三","123");//前台传递过来的用户名和密码

        try {
            subject.login(token);//验证是否登录成功
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
        boolean authenticated = subject.isAuthenticated();
        if (authenticated){//验证是否登录成功
            System.out.println("------成功------");
        }

    }
}
